package ms.unicorn.erml.edit.parts;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LayoutManager;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart;
import org.eclipse.gmf.runtime.diagram.ui.figures.ShapeCompartmentFigure;
import org.eclipse.gmf.runtime.notation.Size;
import org.eclipse.gmf.runtime.notation.View;

import ms.unicorn.diagram.edit.parts.EntityEditPart;
import ms.unicorn.erml.styles.ErmlStyle.CollapsableConstraint;
import ms.unicorn.erml.styles.ErmlStyle.ErmlStylePackage;

public abstract class ContainerEntityEditPart extends EntityEditPart {

	private CollapsableConstraint collapsableConstraint;
	
	public ContainerEntityEditPart(View view) {
		super(view);
	}

	public abstract CompartmentEditPart getCompartmentEditPart();

	public boolean isCollapsed() {
		IFigure fig = getCompartmentEditPart().getFigure();
		if (fig instanceof ShapeCompartmentFigure)
			return !((ShapeCompartmentFigure) fig).isExpanded();
		
		return false;
	}

	public CollapsableConstraint getCollapsableConstraint() {
		if (this.collapsableConstraint == null) {
			Object model = basicGetModel();
			if (hasNotationView()) {
				View view = (View) model;
				collapsableConstraint = (CollapsableConstraint) view.getStyle(ErmlStylePackage.eINSTANCE.getCollapsableConstraint());
			}
		}
		return this.collapsableConstraint;
	}

	public void setCollapse(boolean collapsed) {
		if (getContentPane() != null)
			getContentPane().setVisible(!collapsed);
		CollapsableConstraint sizes = getCollapsableConstraint();
		if (sizes != null) {
			IFigure plate = figures.get(FIGURE_ELEMENT_PLATE);
			Rectangle bnd = plate.getBounds().getCopy();
			if (collapsed)
				bnd.setSize(sizes.getCollapsedDimension());
			else
				bnd.setSize(sizes.getExpandedDimension());
			updateBound(plate, bnd);
		}
	}

	protected void updateBound(IFigure plate, Rectangle bound) {
		LayoutManager lm = plate.getParent().getLayoutManager();
		lm.setConstraint(plate, bound);
	}

	protected Size getActiveSize() {
		CollapsableConstraint constraint = getCollapsableConstraint();
		Size size = null;
		if (constraint != null)
			size = isCollapsed() ? constraint.getCollapsedSize() : constraint.getExpandedSize();
		return size;
	}

	@Override
	protected void handleNotificationEvent(Notification notification) {
		super.handleNotificationEvent(notification);
		Object feature = notification.getFeature();
		//System.out.println(feature);
		if (feature instanceof EAttribute) {
			EAttribute attr = (EAttribute) feature;
			String attrName = attr.getName();
			if ("width".equals(attrName)) {
				Size size = getActiveSize();
				if (size != null)
					size.setWidth(notification.getNewIntValue());
			}
			if ("height".equals(attrName)) {
				Size size = getActiveSize();
				if (size != null)
					size.setHeight(notification.getNewIntValue());
			}
		}
	}

}